package com.tdam2013.grupo07.loader;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.database.Cursor;

import com.tdam2013.grupo07.data.DatabaseHelper;
import com.tdam2013.grupo07.data.DatabaseHelperSingleton;

public class ConectividadCursorLoader extends AsyncTaskLoader<Cursor>
{
	private String selection;
	private String[] selectionArgs;
	private String order;
    private Cursor mData;

	
	public ConectividadCursorLoader(Context context, String selection, String[] selectionArgs, String order) 
	{
        super(context);
        this.selection=selection;
        this.selectionArgs=selectionArgs;
        this.order=order;
        mData=null;
	}

	@Override
	public Cursor loadInBackground() {
		DatabaseHelper dbHelper=DatabaseHelperSingleton.getInstance(this.getContext()).getDatabaseHelper();
		return dbHelper.seleccionarRegistrosDeConectividad(selection, selectionArgs, order);
	}
	
	@Override
	  public void deliverResult(Cursor cursor) {
	    if (isReset()) {
	      // The Loader has been reset; ignore the result and invalidate the data.
	      releaseResources(cursor);
	      return;
	    }
	 
	    // Hold a reference to the old data so it doesn't get garbage collected.
	    // We must protect it until the new data has been delivered.
	    Cursor oldData = mData;
	    mData = cursor;
	 
	    if (isStarted()) {
	      // If the Loader is in a started state, deliver the results to the
	      // client. The superclass method does this for us.
	      super.deliverResult(cursor);
	    }
	 
	    // Invalidate the old data as we don't need it any more.
	    if (oldData != null && oldData != cursor) {
	      releaseResources(oldData);
	    }
	  }
	
	private void releaseResources(Cursor cursor) {
	    if(cursor!=null && !cursor.isClosed())
	    	cursor.close();
	  }
	
	  @Override
	  protected void onStartLoading() {
	    if (mData != null) {
	      // Deliver any previously loaded data immediately.
	      deliverResult(mData);
	    }
	 
	    if (takeContentChanged() || mData == null) {
	      // When the observer detects a change, it should call onContentChanged()
	      // on the Loader, which will cause the next call to takeContentChanged()
	      // to return true. If this is ever the case (or if the current data is
	      // null), we force a new load.
	      forceLoad();
	    }
	  }
	 
	  @Override
	  protected void onStopLoading() {
	    // The Loader is in a stopped state, so we should attempt to cancel the 
	    // current load (if there is one).
	    cancelLoad();
	 
	    // Note that we leave the observer as is. Loaders in a stopped state
	    // should still monitor the data source for changes so that the Loader
	    // will know to force a new load if it is ever started again.
	  }
	 
	  @Override
	  protected void onReset() {
	    // Ensure the loader has been stopped.
	    onStopLoading();
	 
	    // At this point we can release the resources associated with 'mData'.
	    if (mData != null) {
	      releaseResources(mData);
	      mData = null;
	    }
	 
	  }
	 
	  @Override
	  public void onCanceled(Cursor data) {
	    // Attempt to cancel the current asynchronous load.
	    super.onCanceled(data);
	 
	    // The load has been canceled, so we should release the resources
	    // associated with 'data'.
	    releaseResources(data);
	  }
}
